package com.soujiayi.adapter;

import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.soujiayi.activity.R;
import com.soujiayi.model.ProductSelectItem;
import com.soujiayi.util.Constants;

public class ProductSelectAdapter extends BaseExpandableListAdapter {

	private LayoutInflater mInflater;
	private List<Integer> group;
	private List<? extends List<ProductSelectItem>> data;

	private String TYPE;
	private String ATTRIBUTE;
	private String SELLER;
	private String PRICE_SCOPE;
	private String BRAND;
	
	public boolean isPriceScopeExpand = false;

	public ProductSelectAdapter(Context context,List<Integer> group,
			List<? extends List<ProductSelectItem>> data) {
		this.group = group;
		this.data = data;
		this.TYPE = context.getResources().getString(
				R.string.product_select_type);
		this.ATTRIBUTE = context.getResources().getString(
				R.string.product_select_attribute);
		this.SELLER = context.getResources().getString(
				R.string.product_select_seller);
		this.PRICE_SCOPE = context.getResources().getString(
				R.string.product_select_price_scope);
		this.BRAND = context.getResources().getString(
				R.string.product_select_brand);

		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		System.out.println("getChild:" + groupPosition + "  " + childPosition);
		if(groupPosition == Constants.PRODUCT_SELECT_PRICE_SCOPE){
			return null;
		}
		return data.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		View view = convertView;
		ChildViewHolder holder;
		
		if (view == null) {
			view = mInflater.inflate(R.layout.product_select_child, null);
			holder = new ChildViewHolder();
			holder.selectItem = (TextView) view.findViewById(R.id.product_select_selectItem);
			holder.selectChecked = (TextView) view.findViewById(R.id.product_select_selectChecked);
			view.setTag(holder);
		}else{
			holder = (ChildViewHolder)view.getTag();
		}
		bindChildView(holder, groupPosition, childPosition);


		return view;
	}

	private void bindChildView(ChildViewHolder holder, int groupPosition,
			int childPosition) {
		ProductSelectItem item = data.get(groupPosition).get(childPosition);
		holder.selectItem.setText(item.getName() == null ? "" : item.getName());
		holder.selectChecked.setVisibility(item.isSelected() ? 0 : 8);
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return data.get(groupPosition) == null ? 0: data.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return group.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return group.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View view = convertView;
		GroupViewHolder holder;
		if (view == null) {
			view = mInflater.inflate(R.layout.product_select_group, null);
			holder = new GroupViewHolder();
			holder.groupName = (TextView) view.findViewById(R.id.product_select_groupName);
			holder.groupImg = (ImageView) view.findViewById(R.id.product_select_groupImg);
			view.setTag(holder);
		} else {
			holder = (GroupViewHolder) view.getTag();
		}
		bindGroupView(holder, groupPosition, isExpanded);
		
		if(groupPosition == Constants.PRODUCT_SELECT_PRICE_SCOPE){
			this.isPriceScopeExpand = isExpanded;
		}

		return view;
	}

	private void bindGroupView(GroupViewHolder holder, int groupPosition,
			boolean isExpanded) {
		int index = (Integer) getGroup(groupPosition);
		switch (index) {
		case Constants.PRODUCT_SELECT_TYPE:
			holder.groupName.setText(this.TYPE);
			break;
		case Constants.PRODUCT_SELECT_ATTRIBUTE:
			holder.groupName.setText(this.ATTRIBUTE);
			break;
		case Constants.PRODUCT_SELECT_SELLER:
			holder.groupName.setText(this.SELLER);
			break;
		case Constants.PRODUCT_SELECT_PRICE_SCOPE:
			holder.groupName.setText(this.PRICE_SCOPE);
			break;
		case Constants.PRODUCT_SELECT_BRAND:
			holder.groupName.setText(this.BRAND);
			break;
		default:
			break;
		}
		if (isExpanded) {
			holder.groupImg.setImageResource(R.drawable.arrow_up);
		} else {
			holder.groupImg.setImageResource(R.drawable.arrow_down);
		}
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		if(groupPosition == Constants.PRODUCT_SELECT_PRICE_SCOPE){
			return false;
		}
		return true;
	}

	private static class GroupViewHolder {
		TextView groupName;
		ImageView groupImg;
	}

	private static class ChildViewHolder {
		TextView selectItem;
		TextView selectChecked;
	}


}
